package org.brakepedal.server.support.controller.responseBodyAdvice;

import jakarta.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * Date: 2023/6/29 11:11
 *
 */
//@RestControllerAdvice/*(annotations = PageBody.class, basePackages = "com.pdl.chatroomjava.controller")*/
//@Order(8001)
public class NotFoundResetAdvice implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(@NotNull MethodParameter returnType, @NotNull Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, @NotNull MethodParameter returnType,
                                  @NotNull MediaType selectedContentType, @NotNull Class selectedConverterType,
                                  @NotNull ServerHttpRequest request, @NotNull ServerHttpResponse response) {
        ServletServerHttpResponse httpResponse = (ServletServerHttpResponse) response;
        HttpServletResponse servletResponse = httpResponse.getServletResponse();
        if (servletResponse.getStatus() == HttpServletResponse.SC_NOT_FOUND) {
            servletResponse.setStatus(HttpServletResponse.SC_OK);
        }
        return body;
    }
}
